package com.changcheng.controller;


import com.alibaba.fastjson.JSON;
import com.changcheng.pojo.CarouselMap;
import com.changcheng.pojo.Picture;
import com.changcheng.pojo.User;
import com.changcheng.service.CarouselMapService;
import com.changcheng.service.FileService;
import com.changcheng.utils.PageUtil;
import com.changcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "轮播图上传接口")
public class CarouselMapController {

    @Autowired
    private CarouselMapService carouselMapService;
    @Autowired
    private FileService fileService;

    //    添加产品
    @PutMapping("/addCarouselMap")
    @ApiOperation(value = "添加产品")
//    public boolean addCarouselMap(CarouselMap carouselMap, MultipartFile file, Model model) {
    public Map<String, Object> a(CarouselMap carouselMap, MultipartFile file, Model model) {
        //获取上传文件
        Map map = new HashMap();
        if (file.getSize() > 1024 * 1024 * 10) {
            model.addAttribute("msg", "文件太大");
        } else {

            String url = fileService.upload(file);
            if (url.equals(null)) {
                model.addAttribute("msg", "图片上传错误");
            } else {
                Subject subject = SecurityUtils.getSubject();
                User user = (User) subject.getPrincipal();
//            System.out.println(user.getUsername());
//                carouselMap.setUser_id(user.getUsername());
                carouselMap.setUser_id(user.getId());
                carouselMap.setImg(url);
                carouselMap.setCreat_time(LocalDate.now());
                carouselMapService.addCarouselMap(carouselMap);
                map.put("carouselMap", carouselMap);
            }
        }
        return map;
    }

    @ApiOperation(value = "删除 (根据id删除)")
    @GetMapping("/deleteCarouselMap")
    public R deleteCarouselMap(Integer id) {
        boolean b = carouselMapService.deleteCarouselMap(id);
        if (b == true)
            return R.ok();
        else
            return R.error();
    }

    //修改
    @ApiOperation(value = "修改状态(根据id修改)")
    @GetMapping("/updateCarouselMapStatus")
    public R update(CarouselMap carouselMap) {
        carouselMapService.updateCarouselMapStatus(carouselMap);
        return R.ok();
    }

    //查询所有的轮播图(客户前台)
    @ApiOperation(value = "查询轮播图,可根据状态查询")
    @ResponseBody
    @GetMapping("/selectAllCarouselMap")
    public String selectAllCarouselMap(CarouselMap carouselMap) {
        HashMap<String, Object> hashMap = new HashMap<>();
        List<CarouselMap> selectAllCarouselMap = carouselMapService.select(carouselMap);
        if (selectAllCarouselMap != null) {
            hashMap.put("data", selectAllCarouselMap);
            hashMap.put("flag", 0);
        } else {
            hashMap.put("flag", 1);
        }
        return JSON.toJSONString(hashMap);
    }
}
